import axios from 'axios'
import config from './config'

const statuCode = config.config.code;
const routerNv = config.config.requestUrlnv;
const routerVe = config.config.requestUrlve;

const userInfo = {
    userphone: '',
    token: '',
    username: '',
};

function catchMessage (e) {
    let code = 'Timeout';
    if (e.code) {
        code = e.code;
    }
    return {code, msg: e.msg || '网路错误'}
}

function setInfo () {
    const [userphone, token, username] = [...arguments];
    userInfo['userphone'] = userphone;
    userInfo['token'] = token;
    userInfo['username'] = username;
}

function setStorage () {
    try {
        const [userphone, token, username] = [...arguments];
        setInfo(userphone, token, username);
        localStorage.setItem("chat_userphone", userInfo.userphone);
        localStorage.setItem("chat_token", userInfo.token);
        localStorage.setItem("chat_username", userInfo.username);
    } catch (e) {
        console.log('游览器暂不支持localStorage')
    }
}

function getStorage () {
    try {
        if (localStorage.getItem("userphone")) {
            const userphone = localStorage.getItem("chat_userphone");
            const token = localStorage.getItem("chat_token");
            const username = localStorage.getItem("chat_username");
            setInfo(userphone, token, username)
        }
    } catch (e) {
        console.log('游览器暂不支持localStorage')
    }
};

function clearStroage () {
    try {
        setStorage('', '', '')
    } catch (e) {
        console.log('游览器暂不支持localStorage')
    }
}

function login (msg = {}) {
    const {userphone, password} = {...msg};
    const url = routerNv['login'];
    const para = {
        userphone,
        password,
    };
    return axios.post(url, para)
        .then(res => {
            if (res.data.errcode === statuCode['success']) {
                setStorage(res.data.data.userphone, res.data.data.token, res.data.data.username);
                return Promise.resolve(res.data)
            } else {
                return Promise.reject({code: res.data.errcode, msg: res.data.errmsg})
            }
        })
        .catch((e) => {
            return Promise.reject(catchMessage(e))
        })
}

function register (msg = {}) {
    const {username, password, repassword, userphone, code} = {...msg};
    const url = routerNv['register'];
    const para = {
        username,
        password,
        userphone,
        repassword,
        code
    };
    return axios.post(url, para)
        .then(res => {
            if (res.data.errcode === statuCode['success']) {
                return Promise.resolve()
            } else {
                return Promise.reject({code: res.data.errcode, msg: res.data.errmsg})
            }
        })
        .catch(e => {
            return Promise.reject(catchMessage(e))
        })
}

function checktoken () {
    const url = routerVe['checktoken'];
    return axios.post(url, {})
        .then(res => {
            if (res.data.errcode === statuCode['success']) {
                return Promise.resolve()
            } else {
                return Promise.reject({code: res.data.errcode, msg: res.data.errmsg})
            }
        })
        .catch(e => {
            return Promise.reject(catchMessage(e))
        })
}

function getVerificationCode (msg = {}) {
    const {userphone} = {...msg};
    const url = routerNv['verificationCode'];

    const para = {userphone};
    return axios.post(url, para)
        .then(res => {
            if (res.data.errcode === statuCode['success']) {
                return Promise.resolve(res.data.data)
            } else {
                return Promise.reject({code: res.data.errcode, msg: res.data.errmsg})
            }
        })
        .catch(e => {
            return Promise.reject(catchMessage(e))
        })
}

function updatePassword (msg = {}) {
    const {userphone, password, code} = {...msg};
    const url = routerNv['updatePassword'];

    const para = {
        userphone,
        password,
        code
    };
    return axios.post(url, para)
        .then(res => {
            if (res.data.errcode === statuCode['success']) {
                return Promise.resolve(res.data.data)
            } else {
                return Promise.reject({code: res.data.errcode, msg: res.data.errmsg})
            }
        })
        .catch(e => {
            return Promise.reject(catchMessage(e))
        })
}

function updateUsername (msg = {}) {
    const {userphone, username} = {...msg};
    const url = routerVe['updateUsername'];

    const para = {
        userphone,
        username
    };
    return axios.post(url, para)
        .then(res => {
            if (res.data.errcode === statuCode['success']) {
                return Promise.resolve(res.data.data)
            } else {
                return Promise.reject({code: res.data.errcode, msg: res.data.errmsg})
            }
        })
        .catch(e => {
            return Promise.reject(catchMessage(e))
        })
}

function collectData () {
    let url = 'http://39.106.71.133:8080/user/ip/getip';
    axios.post(url, {})
}

export default {
    userInfo,
    login,
    register,
    getStorage,
    checktoken,
    clearStroage,
    updatePassword,
    updateUsername,
    getVerificationCode,
    collectData
}